/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package magicthegathering;

import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;
import java.io.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
import java.applet.*;
import java.util.Iterator;
import java.util.List;
import java.util.Collections;
import java.util.ArrayList;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.imageio.*;

//Necessary Globals to implement later
class MyCanvas extends JComponent {

    String Phase;
    MTG_Model model;
    int cardx = 0, cardy = 0; //determines which card is selected atm
    boolean cardxyoutline = false;
    int Turn = 1;
    MTG_Model.Card currentCardSelected = null;
    
    public String filegrab(){
        File dir1 = new File (".");
        String filepath = "";
        try {
            filepath = dir1.getCanonicalPath();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        return filepath;
    }

    String getPhase() {
        return Phase;
    }

    int getTurn() {
        return Turn;
    }

    void setTurn(int i) {
        Turn = i;
    }

    public void paint(Graphics g) {
        for (int i = 0; i < model.player1.hand_classes.size(); i++) {
            model.player1.hand_classes.get(i).xpos = i;
        }
        //Draw the playing field
        Graphics2D g2 = (Graphics2D)g;
        g2.setFont(new Font("SansSerif", Font.BOLD, 12));
        FontMetrics fm = g.getFontMetrics();
        g2.setColor(Color.BLACK);
        Color bgcolor = new Color(60,179,113);
        String file = filegrab() + "\\bg.png";
        GradientPaint a = new GradientPaint(450, 300, bgcolor, 900, 600, Color.lightGray);
        g2.setPaint(a);
        g2.drawRect(25, 25, 900, 600);
        //Inner Triangle
        g.fillRect(25, 25, 900, 600);
        g.drawRect(30, 30, 890, 590);
        g.setColor(bgcolor);
        //g.fillRect(30, 30, 890, 590);

        //DrawOutline of card if necessary
        if (cardxyoutline) {
            g.setColor(Color.YELLOW);
            DrawOutline(g);
        }

        //10 Boxes (5 for player 1, 5 for player 2)

        /*Player 1 boxes (top)*/
        //Boxes need to be 8 X 11 - cards
        g.setColor(Color.BLACK);
        g.drawRect(40, 55, 80, 110);
        g.drawRect(145, 55, 80, 110);
        g.drawRect(250, 55, 80, 110);
        g.drawRect(355, 55, 80, 110);
        g.drawRect(460, 55, 80, 110);
        g.drawRect(565, 55, 80, 110);
        g.drawRect(670, 55, 80, 110);

        /*Player 1 boxes (bottom)*/
        //Boxes need to be 8 X 11 - cards
        g.drawRect(40, 195, 80, 110);
        g.drawRect(145, 195, 80, 110);
        g.drawRect(250, 195, 80, 110);
        g.drawRect(355, 195, 80, 110);
        g.drawRect(460, 195, 80, 110);
        g.drawRect(565, 195, 80, 110);
        g.drawRect(670, 195, 80, 110);


        //Side Legend
        g.fillRect(760, 25, 5, 600);
        /*Top Middle Bar*/
        //int x, int y, int width, int length
        g.fillRect(30, 180, 890, 5);
        //Middle Middle bar
        g.fillRect(30, 320, 300, 1);
        g.drawRect(330, 310, 130, 20);
        g.drawString("The Action Button", 345, 325);
        g.fillRect(460, 320, 465, 1);
        //Lower Middle Bar
        g.fillRect(30, 460, 890, 5);

        //Information for side  legend
        g.drawString("Whose Turn?", 780, 195);
        //Next Button
        g.drawString("NEXT PHASE BUTTON", 780, 300);
        //Phase Information:
        PhaseInformation(g);

        g.drawRect(770, 280, 140, 30);

        g.drawString("Card Stats:", 780, 335);

        //Player 1 stuff
        g.drawString("Player 1", 780, 40);
        g.drawString("Life:", 800, 60);
        g.drawString("Hand:", 800, 80);
        g.drawString("Deck:", 800, 100);
        g.drawString("Red:", 800, 120);
        g.drawString("Green:", 850, 120);
        g.drawString("Blue:", 800, 140);
        g.drawString("White:", 850, 140);
        g.drawString("Black:", 800, 160);

        //Player 2 stuff
        g.drawString("Player 2", 780, 475);
        g.drawString("Life:", 800, 495);
        g.drawString("Hand:", 800, 515);
        g.drawString("Deck:", 800, 535);
        g.drawString("Red:", 800, 555);
        g.drawString("Green:", 850, 555);
        g.drawString("Blue:", 800, 575);
        g.drawString("White:", 850, 575);
        g.drawString("Black:", 800, 595);

        /*Player 2 boxes (bottom)*/
        g.drawRect(40, 335, 80, 110);
        g.drawRect(145, 335, 80, 110);
        g.drawRect(250, 335, 80, 110);
        g.drawRect(355, 335, 80, 110);
        g.drawRect(460, 335, 80, 110);
        g.drawRect(565, 335, 80, 110);
        g.drawRect(670, 335, 80, 110);

        /*Player 2 boxes (bottom)*/
        g.drawRect(40, 487, 80, 110);
        g.drawRect(145, 487, 80, 110);
        g.drawRect(250, 487, 80, 110);
        g.drawRect(355, 487, 80, 110);
        g.drawRect(460, 487, 80, 110);
        g.drawRect(565, 487, 80, 110);
        g.drawRect(670, 487, 80, 110);


        //Draw Card Function
        DrawCards(g);

        //Current status
        DrawStatus(g);

    }

    public void PhaseInformation(Graphics g) {
        Phase = "";
        if (Turn == 1) {
            Phase = "Player 1's Mana Phase";
        }
        if (Turn == 2) {
            Phase = "Player 1's Creature Phase";
        }
        if (Turn == 3) {
            Phase = "Player 1's Spell Phase";
        }
        if (Turn == 4) {
            Phase = "Player 1's Attack Phase";
        }
        if (Turn == 5) {
            Phase = "Player 2's Defend Phase";
        }
        if (Turn == 6) {
            Phase = "Combat Phase";
        }
        if (Turn == 7) {
            Phase = "Player 2's Mana Phase";
        }
        if (Turn == 8) {
            Phase = "Player 2's Creature Phase";
        }
        if (Turn == 9) {
            Phase = "Player 2's Spell Phase";
        }
        if (Turn == 10) {
            Phase = "Player 2's Attack Phase";
        }
        if (Turn == 11) {
            Phase = "Player 1's Defend Phase";
        }
        if (Turn == 12) {
            Phase = "Combat Phase";
        }


        g.drawString(Phase, 780, 215);
    }

    public void CardUpdate() {
        //Updates the card's position in the table
    }
    //Draws the outline of a single card

    public void DrawOutlineCard(Graphics g, List<MTG_Model.Card> hand_classes, MTG_Model.Player player, int yPos) {
        g.fillRect((cardx * 105) + 40 - 5, yPos, 90, 120);
        for (int i = 0; i < hand_classes.size(); i++) {
            if (hand_classes.get(i).getxpos() == cardx) {
                g.setColor(Color.BLACK);
                g.drawString("Name: " + hand_classes.get(i).name, 790, 355);
                if (!hand_classes.get(i).type.equals("Land")) {
                    g.drawString("Cost: " + hand_classes.get(i).costcolor + " " + player.deck_type, 790, 375);
                }
                if (hand_classes.get(i).type.equals("Creature")) {
                    g.drawString("Power/Defence: " + Integer.toString(hand_classes.get(i).attack) + "/" + Integer.toString(hand_classes.get(i).defense), 790, 395);
                }

            }

        }
    }
    //Decides where to draw the outline of a card

    public void DrawOutline(Graphics g) {
        //Refresh Player card list
        for (int i = 0; i < model.player1.hand_classes.size(); i++) {
            model.player1.hand_classes.get(i).updatexpos(i);
        }
        for (int i = 0; i < model.player2.hand_classes.size(); i++) {
            model.player2.hand_classes.get(i).updatexpos(i);
        }


        if (cardy == 0) {
            DrawOutlineCard(g, model.player1.hand_classes, model.player1, 50);
        }
        if (cardy == 1) {
            DrawOutlineCard(g, model.CardsInPlay1, model.player1, 190);
        }
        if (cardy == 2) {
            DrawOutlineCard(g, model.CardsInPlay2, model.player2, 330);
        }
        if (cardy == 3) {
            DrawOutlineCard(g, model.player2.hand_classes, model.player2, 482);
        }
    }

    public void DrawStatus(Graphics g) {
        /*This function takes a current View deck from global and
         * Prints out all the cards in play.
         */
        g.setFont(new Font("SansSerif", Font.BOLD, 12));
        g.setColor(Color.BLACK);
        //Player 1
        g.drawString(Integer.toString(model.player1.life), 836, 60);
        g.drawString(Integer.toString(model.player1.getHand()), 836, 80);
        g.drawString(Integer.toString(model.player1.getDeck()), 836, 100);
        g.drawString(Integer.toString(model.player1.red_tmp), 836, 120);
        g.drawString(Integer.toString(model.player1.green_tmp), 888, 120);
        g.drawString(Integer.toString(model.player1.blue_tmp), 836, 140);
        g.drawString(Integer.toString(model.player1.white_tmp), 888, 140);
        g.drawString(Integer.toString(model.player1.black_tmp), 836, 160);

        //Player 2
        g.drawString(Integer.toString(model.player2.life), 836, 495);
        g.drawString(Integer.toString(model.player2.getHand()), 836, 515);
        g.drawString(Integer.toString(model.player2.getDeck()), 836, 535);
        g.drawString(Integer.toString(model.player2.red_tmp), 836, 555);
        g.drawString(Integer.toString(model.player2.green_tmp), 888, 555);
        g.drawString(Integer.toString(model.player2.blue_tmp), 836, 575);
        g.drawString(Integer.toString(model.player2.white_tmp), 888, 575);
        g.drawString(Integer.toString(model.player2.black_tmp), 836, 595);

    }

    public void DrawCards(Graphics g) {
        /*This function takes a current View deck from global and
         * Prints out all the cards in play.
         */

        /*Draw Cards in each player's hand*/
        //Player 1's Cards (hand)
        for (int i = 0; i < model.player1.hand_classes.size(); i++) {
            DrawCard(g, (40 + 105 * (i)), 55, model.player1.hand_classes.get(i), -1);
            model.player1.hand_classes.get(i).xpos = i;
            model.player1.hand_classes.get(i).ypos = 0;
            if (i == 6) {
                break;
            }
        }

        //Player 2's Cards (hand)
        for (int i = 0; i < model.player2.hand_classes.size(); i++) {
            DrawCard(g, (40 + 105 * (i)), 487, model.player2.hand_classes.get(i), -1);
            model.player2.hand_classes.get(i).xpos = i;
            model.player2.hand_classes.get(i).ypos = 3;
            if (i == 6) {
                break;
            }
        }

        //Player 1's Cards (in play)
        for (int i = 0; i < model.CardsInPlay1.size(); i++) {
            DrawCard(g, (40 + 105 * (i)), 195, model.CardsInPlay1.get(i), 1);
            model.CardsInPlay1.get(i).xpos = i;
            model.CardsInPlay1.get(i).ypos = 1;
            if (i == 6) {
                break;
            }
        }

        //Player 2's Cards (in play)
        for (int i = 0; i < model.CardsInPlay2.size(); i++) {
            DrawCard(g, (40 + 105 * (i)), 335, model.CardsInPlay2.get(i), 2);
            model.CardsInPlay2.get(i).xpos = i;
            model.CardsInPlay2.get(i).ypos = 2;
            if (i == 6) {
                break;
            }
        }


    }
    //Used to display error messages!

    public void MessageDialog(String string) {
        JOptionPane.showMessageDialog(null, string);

    }
    //Funny, Draw is probably gonna be weird. We need a "draw" function to get cards

    public void DrawCard(Graphics g, int xcoor, int ycoor, MTG_Model.Card card, int pos) {
        /*This function takes a class card type and position
         */
        int adjustment = 0; //Adjustment for attacking or defending.
        if (pos == 1) {
            if (card.status == 1) {
                adjustment = 10;
            } else if (card.status == 2) {
                adjustment = -10;
            }
        }
        if (pos == 2) {
            if (card.status == 1) {
                adjustment = -10;
            } else if (card.status == 2) {
                adjustment = 10;
            }
        }
        Graphics2D g2 = (Graphics2D)g;
       // g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);


        g.setFont(new Font("sans-serif", Font.PLAIN, 12));
        //Outside box
        g.setColor(Color.BLACK);
        g.fillRect(xcoor, ycoor + adjustment, 80, 110);
        //Inside box
        g.setColor(Color.GRAY);
        g.fillRect(xcoor + 5, ycoor + 5 + adjustment, 70, 100);
        //Top box
        if (card.type.equals("Land") && card.name.equals("Swamp")) {
            g.setColor(Color.BLACK);
        } else if (card.type.equals("Land") && card.name.equals("Sea")) {
            g.setColor(Color.BLUE);
        } else if (card.type.equals("Land") && card.name.equals("Forest")) {
            g.setColor(new Color(00, 99, 00));
        } else if (card.type.equals("Land") && card.name.equals("Mountain")) {
            g.setColor(Color.RED);
        } else if (card.type.equals("Land") && card.name.equals("Plains")) {
            g.setColor(Color.WHITE);
        } else {
            g.setColor(Color.WHITE);
        }
        g.fillRect(xcoor + 10, ycoor + 10 + adjustment, 60, 50);
        //Lower Box
        g.setColor(Color.WHITE);
        g.fillRect(xcoor + 10, ycoor + 70 + adjustment, 60, 30);
        g.setColor(Color.WHITE);
        g.drawString(card.name, xcoor + 10, ycoor + 10 + adjustment);
        g.setColor(Color.RED);
        if (card.type.equals("Creature")) {
            g.drawString(Integer.toString(card.attack) + "/" + Integer.toString(card.defense), xcoor + 30, ycoor + 80 + adjustment);
        }
        g.drawImage(card.img, xcoor + 10, ycoor + 10 + adjustment, 60, 50, null);
    }
}

public class MagicView implements MTG_View, ActionListener {

    MyCanvas mycanvas;
    MTG_Model model;

    public void valueChanged(int oldValue) {
        mycanvas.repaint();
    }

    public JMenuBar createMenuBar() {
        JMenuBar menuBar;
        JMenuItem menuItem;
        JMenu menu;
        //Create the menu bar.
        menuBar = new JMenuBar();

        //Build the first menu.
        menu = new JMenu("File");
        menu.setMnemonic(KeyEvent.VK_A);
        menu.getAccessibleContext().setAccessibleDescription(
                "The only menu in this program that has menu items");
        menuBar.add(menu);

        //a group of JMenuItems
        menuItem = new JMenuItem("Restart", KeyEvent.VK_T);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_1, ActionEvent.ALT_MASK));
        menuItem.getAccessibleContext().setAccessibleDescription(
                "This doesn't really do anything");
        menuItem.addActionListener(this);
        menu.add(menuItem);

        //Adding a new help menu
        JMenu helpmenu = new JMenu("Help");
        helpmenu.getAccessibleContext().setAccessibleDescription(
                "Help Menu");
        menuBar.add(helpmenu);

        JMenuItem menuItem3 = new JMenuItem("About", KeyEvent.VK_A);
        menuItem3.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                String str = "Creators: Scott Forest Hull and Christopher Craig";
                JOptionPane.showMessageDialog(new JFrame(), str, "About", JOptionPane.PLAIN_MESSAGE);
            }
        });
        menuBar.add(menuItem3);

        JMenuItem menuItem4 = new JMenuItem("Help", KeyEvent.VK_H);
        menuItem4.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                String str = printRules();
                JOptionPane.showMessageDialog(new JFrame(), str, "How to Play", JOptionPane.PLAIN_MESSAGE);
            }
        });
        helpmenu.add(menuItem4);

        return menuBar;
    }

    public void actionPerformed(ActionEvent e) {
        JMenuItem source = (JMenuItem) (e.getSource());
        if (source.getText().equals("Restart")) {
            //Model restart
            System.out.println("Restart Application");
            model = new MTG_Model();
            mycanvas.model = model;
            mycanvas.Turn = 1;
            mycanvas.repaint();
        }
    }

    MagicView(MTG_Model m) {
        model = m;
        model.addObserver(this);
        JFrame window = new JFrame();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setBounds(30, 30, 965, 700);
        mycanvas = new MyCanvas();
        mycanvas.model = m;
        /*Build a menu*/
        JMenuBar menu = createMenuBar();
        window.setJMenuBar(menu);
        /*Add canvas to the content pane*/
        window.getContentPane().add(mycanvas);
        window.setVisible(true);

        //Mouse Listener for clicking and moving cards
        mycanvas.addMouseListener(new MouseAdapter() {

            public void mousePressed(MouseEvent event) {
                int tempx = 0, tempy = 0, i;
                //55, 195, 335, 487

                //Selecting the Hands
                if ((event.getY() > 55 && event.getY() < 165 || event.getY() > 487 && event.getY() < 597 || event.getY() < 305 && event.getY() > 195 || event.getY() < 445 && event.getY() > 335) && event.getX() < 770) {
                    if (event.getX() > 40 && event.getX() < 770) {
                        //Get X, Y slot
                        mycanvas.cardx = (event.getX() - 40) / 105;
                        mycanvas.cardy = (event.getY() - 55) / 136;
                        mycanvas.cardxyoutline = true;
                        //Figure out which card is selected :

                        if (mycanvas.cardy == 0) {
                            mycanvas.currentCardSelected = cardSelected(model.player1.hand_classes, mycanvas.cardx);
                        } else if (mycanvas.cardy == 1) {
                            mycanvas.currentCardSelected = cardSelected(model.CardsInPlay1, mycanvas.cardx);
                        } else if (mycanvas.cardy == 2) {
                            mycanvas.currentCardSelected = cardSelected(model.CardsInPlay2, mycanvas.cardx);
                        } else {
                            mycanvas.currentCardSelected = cardSelected(model.player2.hand_classes, mycanvas.cardx);
                        }

                        mycanvas.repaint();
                    }
                  //Skip Turn Button
                } else if (event.getX() >= 770 && event.getX() <= 910 && event.getY() >= 280 && event.getY() <= 310) {
                    mycanvas.setTurn(mycanvas.getTurn() + 1);
                 //   mycanvas.MessageDialog("Skipping to next phase...");
                    if (mycanvas.getTurn() == 6 || mycanvas.getTurn() == 12) {

                        //Initiate combat phase!
                        combat(mycanvas.getTurn());

                        //Reset cards remaining in play.
                        ResetCardsInPlay();
                        if (mycanvas.getTurn() == 6) {
                            resetMana(1);
                            PhaseUpdater();
                        }
                        if (mycanvas.getTurn() == 12) {
                            resetMana(0);

                        }
                    }
                    if (mycanvas.getTurn() == 13) {
                        mycanvas.setTurn(1);
                        PhaseUpdater();
                    }
                    mycanvas.repaint();

                } //A card is trying to be moved...
                else if (mycanvas.cardxyoutline && event.getX()>= 330 && event.getX() <= 460 && event.getY() >= 310 && event.getY() < 340) {
                    if (mycanvas.cardy == 0 || mycanvas.cardy == 1) {
                        //Refresh Player card list = Note this is some kinda bug!
                        for (i = 0; i < model.player1.hand_classes.size(); i++) {
                            model.player1.hand_classes.get(i).updatexpos(i);
                        }

                        tempx = (event.getX() - 40) / 105;
                        tempy = (event.getY() - 55) / 136;
                        //Go through the player's hand, find the card location

                        //Player 1 mana phase
                        if (mycanvas.getTurn() == 1 && mycanvas.cardy == 0) {

                            for (i = 0; i < model.player1.hand_classes.size(); i++) {
                                if (model.player1.hand_classes.get(i).xpos == mycanvas.cardx) {
                                    //Add mana
                                    if (model.player1.hand_classes.get(i).type.equals("Land")) {
                                        AddManatoPlayer(0, model.player1.hand_classes.get(i).name);
                                        model.player1removeCardHand(i);
                                        model.player1.setHand(model.player1.getHand() - 1);
                                        resetMana(0);
                                        mycanvas.setTurn(2);
                                    } else {
                                        mycanvas.MessageDialog("This is Player 1s Mana Card Phase.  Please place a mana into play, or hit next to continue.");
                                    }
                                }
                            }
                            //Player 1 Creature Phase
                        } else if (mycanvas.getTurn() == 2 && mycanvas.cardy == 0) {
                            for (i = 0; i < model.player1.hand_classes.size(); i++) {
                                if (model.player1.hand_classes.get(i).xpos == mycanvas.cardx) {
                                    if (model.CardsInPlay1.size() < 8) {
                                        //Play Creature Phase
                                        if (model.player1.hand_classes.get(i).type.equals("Creature")) {
                                            boolean placeCard = PutCardIntoPlay(0, model.player1.deck_type, model.player1.hand_classes.get(i).costcolor);
                                            if (placeCard) {
                                                model.CardsInPlay1AddCard(model.player1.hand_classes.get(i));
                                                model.player1removeCardHand(i);
                                                model.player1.setHand(model.player1.getHand() - 1);
                                            } else {
                                                mycanvas.MessageDialog("Not enough mana");
                                            }
                                        } else {
                                            mycanvas.MessageDialog("This is Player 1s Creature Card Phase.  Please place a creature into play, or hit next to continue.");
                                        }
                                    } else {
                                        mycanvas.MessageDialog("Too many creature cards in play.  Can not place onto the field.");
                                    }
                                }
                            }
                            //Player 1 Spell Phase
                        } else if (mycanvas.getTurn() == 3 && mycanvas.cardy == 0) {
                            for (i = 0; i < model.player1.hand_classes.size(); i++) {
                                if (model.player1.hand_classes.get(i).xpos == mycanvas.cardx) {
                                    //Play Spell Phase
                                    if (model.player1.hand_classes.get(i).type.equals("Spell")) {
                                        boolean placeCard = PutCardIntoPlay(0, model.player1.deck_type, model.player1.hand_classes.get(i).costcolor);
                                        if (placeCard) {
                                            spellDamage(model.player1.hand_classes.get(i), 1);
                                            model.player1removeCardHand(i);
                                            model.player1.setHand(model.player1.getHand() - 1);
                                        } else {
                                            mycanvas.MessageDialog("Not enough mana");
                                        }
                                    } else {
                                        mycanvas.MessageDialog("This is Player 1s Spell Card Phase.  Please place a spell into play, or hit next to continue.");
                                    }
                                }
                            }
                            //Player 1 Attack Phase
                        } else if (mycanvas.getTurn() == 4 && mycanvas.cardy == 1) {
                            for (i = 0; i < model.CardsInPlay1.size(); i++) {
                                if (i == mycanvas.cardx) {
                                    //Declare Attackers Phase
                                    if (model.CardsInPlay1.get(i).getSickness() == 0) {
                                        if (model.CardsInPlay1.get(i).type.equals("Creature") && model.CardsInPlay1.get(i).getSickness() == 0) {
                                            ChangeCreatureStatus(model.CardsInPlay1.get(i), 1, -1);
                                        } else {
                                            mycanvas.MessageDialog("This is Player 1s Attack Card Phase.  Please select able to creatures to attack, or hit next to continue.");
                                        }
                                    } else {
                                        mycanvas.MessageDialog("This creature has summoning sickness.  Please wait until the next phase to attack");
                                    }
                                }
                            }
                            //Player 1 Defend Phase
                        } else if (mycanvas.getTurn() == 11 && mycanvas.cardy == 1) {
                            for (i = 0; i < model.CardsInPlay1.size(); i++) {
                                if (i == mycanvas.cardx) {
                                    //Declare Blockers Phase
                                    if (model.CardsInPlay1.get(i).type.equals("Creature")) {
                                        mycanvas.MessageDialog("Player 1, please select an attacking creature to block");
                                        for (int j = 0; j < model.CardsInPlay2.size(); j++) {
                                            if (model.CardsInPlay2.get(j).getStatus() == 1) {
                                                int n = JOptionPane.showConfirmDialog(null, "Would you like to block " + model.CardsInPlay2.get(j).name + "?", "An Inane Question", JOptionPane.YES_NO_OPTION);
                                                if (n == 0) {
                                                    ChangeCreatureStatus(model.CardsInPlay1.get(i), 2, j);
                                                    break;
                                                }
                                            }
                                        }

                                    } else {
                                        mycanvas.MessageDialog("This is Player 1s Defend Card Phase.  Please select creatures to Block, or hit next to continue.");
                                    }
                                }
                            }
                        } else {
                            mycanvas.MessageDialog(mycanvas.Phase);
                        }
                    }
                    //Player 2's playbook
                    if (mycanvas.cardy == 3 || mycanvas.cardy == 2) {
                        //Refresh Player card list = Note this is some kinda bug!
                        for (i = 0; i < model.player2.hand_classes.size(); i++) {
                            model.player2.hand_classes.get(i).updatexpos(i);
                        }
                        tempx = (event.getX() - 40) / 105;
                        tempy = (event.getY() - 55) / 136;
                        //Go through the player's hand, find the card location

                        //Player 2 Mana Phase
                        if (mycanvas.getTurn() == 7 && mycanvas.cardy == 3) {
                            for (i = 0; i < model.player2.hand_classes.size(); i++) {
                                if (model.player2.hand_classes.get(i).xpos == mycanvas.cardx) {
                                    //Add mana
                                    if (model.player2.hand_classes.get(i).type.equals("Land")) {
                                        AddManatoPlayer(1, model.player2.hand_classes.get(i).name);
                                        model.player2removeCardHand(i);
                                        model.player2.setHand(model.player2.getHand() - 1);
                                        resetMana(1);
                                        mycanvas.setTurn(8);
                                    } else {
                                        mycanvas.MessageDialog("This is Player 2s Mana Card Phase.  Please place a mana into play, or hit next to continue.");
                                    }
                                }
                            }
                            //Player 2 Creature Phase
                        } else if (mycanvas.getTurn() == 8 && mycanvas.cardy == 3) {
                            for (i = 0; i < model.player2.hand_classes.size(); i++) {
                                if (model.player2.hand_classes.get(i).xpos == mycanvas.cardx) {
                                    if (model.CardsInPlay2.size() < 8) {
                                        //Play Creature Phase
                                        if (model.player2.hand_classes.get(i).type.equals("Creature")) {
                                            boolean placeCard = PutCardIntoPlay(1, model.player2.deck_type, model.player2.hand_classes.get(i).costcolor);
                                            if (placeCard) {
                                                model.CardsInPlay2AddCard(model.player2.hand_classes.get(i));
                                                model.player2removeCardHand(i);
                                                model.player2.setHand(model.player2.getHand() - 1);
                                            } else {
                                                mycanvas.MessageDialog("Not enough mana");
                                            }
                                        } else {
                                            mycanvas.MessageDialog("This is Player 2s Creature Card Phase.  Please place a creature into play, or hit next to continue.");
                                        }
                                    } else {
                                        mycanvas.MessageDialog("Too many creature cards in play.  Unable to place");
                                    }
                                }
                            }
                            //Player 2 Spell Phase
                        } else if (mycanvas.getTurn() == 9 && mycanvas.cardy == 3) {
                            for (i = 0; i < model.player2.hand_classes.size(); i++) {
                                if (model.player2.hand_classes.get(i).xpos == mycanvas.cardx) {
                                    //Play Spell Phase
                                    if (model.player2.hand_classes.get(i).type.equals("Spell")) {
                                        boolean placeCard = PutCardIntoPlay(1, model.player2.deck_type, model.player2.hand_classes.get(i).costcolor);
                                        if (placeCard) {
                                            spellDamage(model.player2.hand_classes.get(i), 2);
                                            model.player2removeCardHand(i);
                                            model.player2.setHand(model.player2.getHand() - 1);
                                        } else {
                                            mycanvas.MessageDialog("Not enough mana");
                                        }
                                    } else {
                                        mycanvas.MessageDialog("This is Player 1s Spell Card Phase.  Please place a spell into play, or hit next to continue.");
                                    }
                                }
                            }
                            //Player 2 Attack Phase
                        } else if (mycanvas.getTurn() == 10 && mycanvas.cardy == 2) {
                            for (i = 0; i < model.CardsInPlay2.size(); i++) {
                                if (i == mycanvas.cardx) {
                                    //Declare Attackers Phase
                                    if (model.CardsInPlay2.get(i).getSickness() == 0) {
                                        if (model.CardsInPlay2.get(i).type.equals("Creature")) {
                                            ChangeCreatureStatus(model.CardsInPlay2.get(i), 1, -1);
                                        } else {
                                            mycanvas.MessageDialog("This is Player 2s Attack Card Phase.  Please select creatures able to attack, or hit next to continue.");
                                        }
                                    } else {
                                        mycanvas.MessageDialog("This creature has summoning sickness.  Please wait until the next phase to attack");
                                    }
                                }
                            }
                            //Player 2 Defend Phase
                        } else if (mycanvas.getTurn() == 5 && mycanvas.cardy == 2) {

                            for (i = 0; i < model.CardsInPlay2.size(); i++) {
                                if (i == mycanvas.cardx) {
                                    //Declare Blockers Phase
                                    if (model.CardsInPlay2.get(i).type.equals("Creature")) {
                                    //    mycanvas.MessageDialog("Player 2, please select an attacking creature to block");
                                        for (int j = 0; j < model.CardsInPlay1.size(); j++) {
                                            if (model.CardsInPlay1.get(j).getStatus() == 1) {
                                                int n = JOptionPane.showConfirmDialog(null, "Would you like to block " + model.CardsInPlay1.get(j).name + "?", "An Inane Question", JOptionPane.YES_NO_OPTION);

                                                if (n == 0) {
                                                    ChangeCreatureStatus(model.CardsInPlay2.get(i), 2, j);
                                                    break;
                                                }
                                            }
                                        }

                                    } else {
                                        mycanvas.MessageDialog("This is Player 1s Attack Card Phase.  Please select creatures to attack, or hit next to continue.");
                                    }
                                }
                            }
                        } else {
                            mycanvas.MessageDialog(mycanvas.Phase);
                        }
                    }
                    mycanvas.cardxyoutline = false;
                    mycanvas.repaint();
                } else {
                    mycanvas.cardxyoutline = false;
                }
            }
        });

    }

    //sets the status of a creature card
    public void ChangeCreatureStatus(MTG_Model.Card card, int status, int block) {
        card.setBlock(block);
        card.setStatus(status);
    }

    //Draws Cards for each new beginning phase
    public void PhaseUpdater() {
        int turn = mycanvas.getTurn();
        if (turn == 1) {
            model.Draw(1, model.player1.deck_classes, model.player1.hand_classes);
            model.player1.setHand(model.player1.getHand() + 1);
            model.player1.setDeck(model.player1.getDeck() - 1);
        }
        if (turn == 6) {
            model.Draw(1, model.player2.deck_classes, model.player2.hand_classes);
            model.player2.setHand(model.player2.getHand() + 1);
            model.player2.setDeck(model.player2.getDeck() - 1);
        }
    }

    //Returns true if enough mana to cast.  Otherwise false
    public boolean PutCardIntoPlay(int p, String land, int cost) {
        if (p == 0) {
            if (land.equals("Forest")) {
                if (model.player1.getGreen_tmp() >= cost) {
                    model.player1.setGreen_tmp(model.player1.getGreen_tmp() - cost);
                    return true;
                }
                return false;
            }
            if (land.equals("Sea")) {
                if (model.player1.getBlue_tmp() >= cost) {
                    model.player1.setBlue_tmp(model.player1.getBlue_tmp() - cost);
                    return true;
                }
                return false;
            }
            if (land.equals("Swamp")) {
                if (model.player1.getBlack_tmp() >= cost) {
                    model.player1.setBlack_tmp(model.player1.getBlack_tmp() - cost);
                    return true;
                }
                return false;
            }
            if (land.equals("Mountain")) {
                if (model.player1.getRed_tmp() >= cost) {
                    model.player1.setRed_tmp(model.player1.getRed_tmp() - cost);
                    return true;
                }
                return false;
            }
            if (land.equals("Plains")) {
                if (model.player1.getWhite_tmp() >= cost) {
                    model.player1.setWhite_tmp(model.player1.getWhite_tmp() - cost);
                    return true;
                }
                return false;
            }
            return false;
        }
        if (p == 1) {

            if (land.equals("Forest")) {
                if (model.player2.getGreen_tmp() >= cost) {
                    model.player2.setGreen_tmp(model.player2.getGreen_tmp() - cost);
                    return true;
                }
                return false;
            }
            if (land.equals("Sea")) {
                if (model.player2.getBlue_tmp() >= cost) {
                    model.player2.setBlue_tmp(model.player2.getBlue_tmp() - cost);
                    return true;
                }
                return false;
            }
            if (land.equals("Swamp")) {

                if (model.player2.getBlack_tmp() >= cost) {
                    model.player2.setBlack_tmp(model.player2.getBlack_tmp() - cost);

                    return true;
                }
                return false;
            }
            if (land.equals("Mountain")) {
                if (model.player2.getRed_tmp() >= cost) {
                    model.player2.setRed_tmp(model.player2.getRed_tmp() - cost);
                    return true;
                }
                return false;
            }
            if (land.equals("Plains")) {
                if (model.player2.getWhite_tmp() >= cost) {
                    model.player2.setWhite_tmp(model.player2.getWhite_tmp() - cost);
                    return true;
                }
                return false;
            }
            return false;
        }
        return false;
    }

    //Resets tmp mana to full after the draw phase
    public void resetMana(int p) {
        if (p == 0) {
            model.player1.setBlack_tmp(model.player1.black);
            model.player1.setBlue_tmp(model.player1.blue);
            model.player1.setRed_tmp(model.player1.red);
            model.player1.setGreen_tmp(model.player1.green);
            model.player1.setWhite_tmp(model.player1.white);
        }
        if (p == 1) {

            model.player2.setBlack_tmp(model.player2.black);
            model.player2.setBlue_tmp(model.player2.blue);
            model.player2.setRed_tmp(model.player2.red);
            model.player2.setGreen_tmp(model.player2.green);
            model.player2.setWhite_tmp(model.player2.white);
        }
    }

    //Adds mana to respective player
    public void AddManatoPlayer(int p, String land) {
        if (p == 0) {
            if (land.equals("Forest")) {
                model.player1.green = model.player1.green + 1;
            }
            if (land.equals("Swamp")) {
                model.player1.black = model.player1.black + 1;
            }
            if (land.equals("Mountain")) {
                model.player1.red = model.player1.red + 1;
            }
            if (land.equals("Plains")) {
                model.player1.white = model.player1.white + 1;
            }
            if (land.equals("Sea")) {
                model.player1.blue = model.player1.blue + 1;
            }

        } else {
            if (land.equals("Forest")) {
                model.player2.green = model.player2.green + 1;
            }
            if (land.equals("Swamp")) {
                model.player2.black = model.player2.black + 1;
            }
            if (land.equals("Mountain")) {
                model.player2.red = model.player2.red + 1;
            }
            if (land.equals("Plains")) {
                model.player2.white = model.player2.white + 1;
            }
            if (land.equals("Sea")) {
                model.player2.blue = model.player2.blue + 1;
            }
        }
    }

    //Print the rules for game
    public String printRules() {
        File file = new File("howtoplay.txt");
        String tmp = "";
        String result = "";
        MTG_Functions f = new MTG_Functions();
        List<String> list = f.readfile("howtoplay.txt");
        for (int i = 0; i < list.size(); i++) {
            tmp = tmp + '\n' + list.get(i);
        }
        return tmp;
    }

    //Returns the card selected from a list at index X
    public MTG_Model.Card cardSelected(List<MTG_Model.Card> cards, int x) {

        for (int i = 0; i < cards.size(); i++) {
            if (i == x) {
                return cards.get(i);
            }
        }
        return null;
    }

    //Resets the attack and defense of the cards in play and summoning sickness
    public void ResetCardsInPlay() {
        for (int i = 0; i < model.CardsInPlay1.size(); i++) {
            model.CardsInPlay1.get(i).setAttack_tmp(model.CardsInPlay1.get(i).attack);
            model.CardsInPlay1.get(i).setSickness(0);
            model.CardsInPlay1.get(i).setDefense_tmp(model.CardsInPlay1.get(i).defense);
            model.CardsInPlay1.get(i).setStatus(0);
            model.CardsInPlay1.get(i).setBlock(-1);


        }
        for (int i = 0; i < model.CardsInPlay2.size(); i++) {
            model.CardsInPlay2.get(i).setAttack_tmp(model.CardsInPlay2.get(i).attack);
            model.CardsInPlay2.get(i).setDefense_tmp(model.CardsInPlay2.get(i).defense);
            model.CardsInPlay2.get(i).setSickness(0);
            model.CardsInPlay2.get(i).setStatus(0);
            model.CardsInPlay2.get(i).setBlock(-1);
        }
    }

    //Deals the combat damage and aftermath.
    public void combat(int turn) {
        System.out.println("ITS COMBAT TIME!");
        int i, j;
        int status = 0, block = 1;
        //Player 1 attacking, player 2 defending
        if (mycanvas.getTurn() == 6) {

            //Deal damage inbetween creatures - BUGGGGG HEREREERERERERERERERE

            for (i = 0; i < model.CardsInPlay2.size(); i++) {
                block = 0;
                if (model.CardsInPlay2.get(i).getStatus() == 2) {
                    System.out.println("Someone is blocking!");
                    for (j = 0; j < model.CardsInPlay1.size(); j++) {

                        if (model.CardsInPlay2.get(i).getBlock() == j) {
                            //Fight for each card's life!!!!
                            status = 0;
                            status = Fight_Cards(model.CardsInPlay1.get(j), model.CardsInPlay2.get(i));
                            System.out.println("Status of battle " + status);
                            model.CardsInPlay2.get(i).setStatus(0);
                            model.CardsInPlay1.get(j).setStatus(0);
                            if (status == 0) {
                                //Do nothing
                                model.CardsInPlay2.get(i).setStatus(0);
                            }
                            if (status == 1) {
                                model.CardsInPlay2.get(i).setStatus(-5);
                            }
                            if (status == 2) {
                                model.CardsInPlay1.get(j).setStatus(-5);
                            }
                            if (status == 3) {
                                model.CardsInPlay2.get(i).setStatus(-5);
                                model.CardsInPlay1.get(j).setStatus(-5);
                            }
                            i = 0;
                            break;
                        }
                    }

                }


            }
            //Remaining creature cards alive get a free chance to attack player
            for (i = 0; i < model.CardsInPlay1.size(); i++) {
                if (model.CardsInPlay1.get(i).getStatus() == 1) {
                    model.player2.setLife(model.player2.getLife() - model.CardsInPlay1.get(i).getAttack_tmp());
                }
            }

        }
        //Player 2 attacking, player 1 defending
        if (mycanvas.getTurn() == 12) {
            //Deal damage inbetween creatures
            for (i = 0; i < model.CardsInPlay1.size(); i++) {
                System.out.println("Status during battle: " + model.CardsInPlay1.get(i).getStatus());
                if (model.CardsInPlay1.get(i).getStatus() == 2) {
                    System.out.println("Someone is blocking!");
                    for (j = 0; j < model.CardsInPlay2.size(); j++) {
                        if (model.CardsInPlay1.get(i).getBlock() == j) {
                            //Fight for each card's life!!!!
                            status = 0;
                            status = Fight_Cards(model.CardsInPlay1.get(i), model.CardsInPlay2.get(j));
                            System.out.println("Status of battle " + status);
                            model.CardsInPlay2.get(j).setStatus(0);
                            model.CardsInPlay1.get(i).setStatus(0);
                            if (status == 0) {
                                //Do nothing
                                model.CardsInPlay2.get(j).setStatus(0);
                            }
                            if (status == 1) {
                                model.CardsInPlay2.get(j).setStatus(-5);
                            }
                            if (status == 2) {
                                model.CardsInPlay1.get(i).setStatus(-5);
                            }
                            if (status == 3) {
                                model.CardsInPlay1.get(i).setStatus(-5);
                                model.CardsInPlay2.get(j).setStatus(-5);
                            }
                            i = 0;
                            break;
                        }
                    }
                }

            }
            //Remaining creature cards alive get a free chance to attack player
            for (i = 0; i < model.CardsInPlay2.size(); i++) {
                if (model.CardsInPlay2.get(i).getStatus() == 1) {
                    model.player1.setLife(model.player1.getLife() - model.CardsInPlay2.get(i).getAttack_tmp());
                }
            }


        }
        //Remove cards in play that are dead
        for (i = 0; i < model.CardsInPlay2.size(); i++) {
            if (model.CardsInPlay2.get(i).getStatus() == -5) {
                model.CardsInPlay2removeCard(i);
            }
        }
        for (i = 0; i < model.CardsInPlay1.size(); i++) {
            if (model.CardsInPlay1.get(i).getStatus() == -5) {
                model.CardsInPlay1removeCard(i);
            }
        }

        //Outcome for combat
        if (model.player1.getLife() <= 0 || model.player1.getDeck() <= 0) {
            mycanvas.MessageDialog("Player 2 wins! Restarting game.");
            MTG_Model model1 = new MTG_Model();
            mycanvas.model = model1;
            mycanvas.setTurn(1);
            mycanvas.repaint();

        } else if (model.player2.getLife() <= 0 || model.player2.getDeck() <= 0) {
            mycanvas.MessageDialog("Player 1 wins! Restarting Game");
            MTG_Model model1 = new MTG_Model();
            mycanvas.model = model1;
            mycanvas.setTurn(1);
            mycanvas.repaint();

        }
    }
    //Determine damange dealt by spells.


    public void spellDamage(MTG_Model.Card Spell, int player) {
        if (player == 1) {
            if (Spell.spell_type.equals("DMGPLR")) {
                model.player2.setLife(model.player2.getLife() - Spell.getAttack_tmp());
            } else if (Spell.spell_type.equals("DMGCTR")) {
                //Loop through the creature cards, damage said card.
                for (int i = 0; i < model.CardsInPlay2.size(); i++) {
                    int n = JOptionPane.showConfirmDialog(null, "Would you like damage " + model.CardsInPlay2.get(i).name + "?", "An Inane Question", JOptionPane.YES_NO_OPTION);
                    //Attack target
                    if (n == 0) {
                        model.CardsInPlay2.get(i).setDefense_tmp(model.CardsInPlay2.get(i).getDefense_tmp() - Spell.getAttack_tmp());
                        if (model.CardsInPlay2.get(i).getDefense_tmp() <= 0) {
                            model.CardsInPlay2removeCard(i);
                        }
                        break;
                    }
                }
            }
        }
        if (player == 2) {
            if (Spell.spell_type.equals("DMGPLR")) {
                model.player1.setLife(model.player1.getLife() - Spell.getAttack_tmp());
            } else if (Spell.spell_type.equals("DMGCTR")) {
                //Loop through the creature cards, damage said card.
                for (int i = 0; i < model.CardsInPlay1.size(); i++) {
                    int n = JOptionPane.showConfirmDialog(null, "Would you like damage " + model.CardsInPlay1.get(i).name + "?", "An Inane Question", JOptionPane.YES_NO_OPTION);
                    //Attack target
                    if (n == 0) {
                        model.CardsInPlay1.get(i).setDefense_tmp(model.CardsInPlay1.get(i).getDefense_tmp() - Spell.getAttack_tmp());
                        if (model.CardsInPlay1.get(i).getDefense_tmp() <= 0) {
                            model.CardsInPlay1removeCard(i);
                        }
                        break;
                    }
                }
            }
        }
        mycanvas.repaint();
    }

    //Return 1 for Monster1 victory, 2 for monster 2 victory, 3 for both dead, 0  for draw
    public int Fight_Cards(MTG_Model.Card Monster1, MTG_Model.Card Monster2) {
        int Monster1status = 0, Monster2status = 0; //1 for dead, 0 for alive
        System.out.println("Battle: " + Monster1.name + " attack: " + Monster1.getAttack_tmp() + " defense " + Monster1.getDefense_tmp());
        System.out.println("with: " + Monster2.name + " attack: " + Monster2.getAttack_tmp() + " defense " + Monster2.getDefense_tmp());
        if (Monster1.getAttack_tmp() >= Monster2.getDefense_tmp()) {
            Monster2status = 1;
        }
        if (Monster2.getAttack_tmp() >= Monster1.getDefense_tmp()) {
            Monster1status = 1;
        }

        if (Monster1status == 1 && Monster2status == 1) {
            return 3;
        } else if (Monster1status == 0 && Monster2status == 1) {
            return 1;
        } else if (Monster1status == 1 && Monster2status == 0) {
            return 2;
        } else {
            return 0;
        }

    }
}
